$(document).ready(function () {
    var validate = $("#editForm").validate({
        debug: true,
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function (form) {
            var jsonData = $(form).serializeObject();
            $.ajax({
                url: "CheckCustomer",
                method: "POST",
                contentType: "application/json;charset=UTF-8",
                async: true,
                dataType: "json",
                data: JSON.stringify(jsonData),
                timeout: timeout,
                success: function (F) {
                    if (!F.Result) {
                        //var G = confirm(F.Message + "，确定需要添加该客户吗？");
                        //if (!G) {
                        //    return;
                        //}
                        alert(F.Message);
                        return;
                    }
                    $.ajax({
                        url: "EditCustomer",
                        method: "POST",
                        contentType: "application/json;charset=UTF-8",
                        async: true,
                        dataType: "json",
                        data: JSON.stringify(jsonData),
                        timeout: timeout,
                        success: function (H) {
                            alert(H.Message);
                            if (H.Result) {
                                select("list");
                                query("keep=true", "QueryCustomer");
                            }
                        }
                    });
                }
            }).fail(function (G, F) {
                alert("浏览器超时未响应，请刷新重试");
            });
            return false;
        },

        rules: {
            Name: {
                required: true
            },
            IdNo: {
                //required: true,
                identityCodeValid: true
            }
        },
        messages: {
            Name: {
                required: "姓名不能为空"
            },
            IdNo: {
                //required: "身份证不能为空"
            }
        }

    });

    function A(D) {
        var E = $("table.list tbody");
        E.empty();
        E.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "CustomerPage",
            dataType: "json",
            data: "page=" + D,
            timeout: timeout,
            success: function (F) {
                update(F);
            }
        }).fail(function (G, F) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    var pagerCustomer = {
        maxRows: 0,
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: A
    };
    $("div.jpager").paginate(pagerCustomer);
    window.update = function (H) {
        var D = $("table.list tbody");
        var M = H.Items;
        $("table.list input.selall").prop("checked", false);
        D.empty();
        var E = H.Page * 10 - 9;
        if (M.length > 0) {
            for (var J = 0; J < M.length; J++) {
                var G = M[J];
                var K = G.Tel || "";
                var L = G.IdNo || "";
                var I = G.Addr || "";
                var F = G.Contact || "";
                D.append('<tr><td><input name=ids class="row" type="checkbox" value="' + G.Id + '"/></td><td>' + G.Name + "</td><td>" + G.Gender + "</td><td>" + L + "</td><td>" + K + "</td><td>" + I + "</td><td>" + F + '</td><td><button data-id="' + G.Id + '" class="edit btn btn-success"><i class="fa fa-edit fa-lg"></i> 编 辑</button></td></tr>');
            }
        } else {
            D.append('<tr><td colspan="20">没有相关记录信息</td></tr>');
        }
        pagerCustomer.maxRows = H.MaxRows;
        pagerCustomer.start = H.Page;
        pagerCustomer.count = H.Total;
        $("div.jpager").paginate(pagerCustomer);
    };

    function C(D) {
        $("div#edit input").val("");
        if (D) {
            $("div#edit input.Id").val(D.Id);
            $("div#edit input.Name").val(D.Name);
            $("div#edit input.Tel").val(D.Tel);
            $("div#edit select.Gender").val(D.Gender);
            $("div#edit input.Birthday").val(D.Birthday);
            $("div#edit input.IdNo").val(D.IdNo);
            $("div#edit input.Addr").val(D.Addr);
            $("div#edit select.BabyGender").val(D.BabyGender);
            $("div#edit input.BabyWeight").val(D.BabyWeight);
            $("div#edit input.BabyBirth").val(D.BabyBirth);
            $("div#edit input.Contact").val(D.Contact);
            $("div#edit input.ContactTel").val(D.ContactTel);
            $("div#edit input.ContactRel").val(D.ContactRel);
            $("div#edit input.Note").val(D.Note);
        }
    }
    $("div.query button.query").click(function (D) {
        query($("div.query form").serialize(), "QueryCustomer");
        return false;
    });
    query($("div.query form").serialize(), "QueryCustomer");
    $("ul.tool button.add").click(function (D) {
        C(null);
        select("edit");
        return false;
    });

    $("button.cancel").click(function (D) {
        validate.resetForm();
        select("list");
        return false;
    });
    $("ul.tool button.delete").click(function (D) {
        if (confirm("您确定要删除所有选中的记录吗？")) {
            var E = $("table.list tbody input.row:checked").serialize();
            $.ajax({
                url: "DeleteCustomers",
                dataType: "json",
                data: E,
                timeout: timeout,
                success: function (F) {
                    alert(F.Message);
                    if (F.Result) {
                        query("keep=true", "QueryCustomer");
                    }
                }
            }).fail(function (G, F) {
                alert("浏览器超时未响应，请刷新重试");
            });
        }
    });
    $("table.list tbody").click(function (D) {
        var E = $(D.target);
        if (E.is("button.delete")) {
            if (confirm("您确定要删除该记录吗？")) {
                var F = E.data("id");
                $.ajax({
                    url: "DeleteCustomer",
                    dataType: "json",
                    data: "Id=" + F,
                    timeout: timeout,
                    success: function (G) {
                        alert(G.Message);
                        if (G.Result) {
                            query("keep=true", "QueryCustomer");
                        }
                    }
                }).fail(function (H, G) {
                    alert("浏览器超时未响应，请刷新重试");
                });
            }
        } else {
            if (E.is("button.edit")) {
                var F = E.data("id");
                $.ajax({
                    url: "GetCustomer",
                    dataType: "json",
                    data: "Id=" + F,
                    timeout: timeout,
                    success: function (G) {
                        C(G);
                        select("edit");
                    }
                }).fail(function (H, G) {
                    alert("浏览器超时未响应，请刷新重试");
                });
            }
        }
    });
});